package com.aaa.dao.impl;

import com.aaa.dao.BaseDao;
import com.aaa.dao.DeptDao;
import com.aaa.entity.SysDeptEntity;


import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class DeptDaoImpl extends BaseDao implements DeptDao {
    @Override
    public List query(int parent_id) {
        String sql="select *from sys_dept where parent_id=? and del_flag=0";
        return this.executeQuery(sql,parent_id);
    }

    @Override
    public List query() {
        String sql="select dept_id,dept_name,(select dept_name from sys_dept d2 where d2.dept_id=d1.parent_id) area_name from sys_dept d1 where del_flag=0 and parent_id>0";
        return this.executeQuery(sql);
    }

    @Override
    public Map queryOne(int dept_id) {
        String sql="select *from sys_dept where dept_id=?";
        List<Map> maps=new ArrayList<>();
        maps=executeQuery(sql,dept_id);

        return maps.size()>0?maps.get(0):null;
    }


    @Override
    public int dept_add(SysDeptEntity dept) {
        String sql="insert into sys_dept(parent_id,dept_name,leader,phone,email, status,del_flag,create_by,create_time,update_by,update_time) " +
                "values(?,?,?,?,?,?,?,?,?,?,?)";
        return this.executeUpdate(sql,dept.getParentId(),dept.getDeptName(),dept.getLeader(),dept.getPhone(),dept.getEmail(),0,0,dept.getCreateBy(),dept.getCreateTime(),dept.getUpdateBy(),dept.getUpdateTime());
    }

    @Override
    public int dept_update(SysDeptEntity dept) {
        String sql="update  sys_dept set parent_id=?,dept_name=?,order_num=?,leader=?,phone=?,email=?, status=?,del_flag=?,update_by=?,update_time=? where dept_id=?";

        return this.executeUpdate(sql,dept.getParentId(),dept.getDeptName(),dept.getOrderNum(),dept.getLeader(),dept.getPhone(),dept.getEmail(),0,0,dept.getUpdateBy(),dept.getUpdateTime(),dept.getDeptId());
    }

    @Override
    public int dept_delete(int dept_id) {
        String sql="update sys_dept set del_flag=1 where dept_id=?";
        return this.executeUpdate(sql,dept_id);
    }


}
